import {
  createBrowserRouter,
  Navigate,
  RouterProvider,
} from "react-router-dom";
import HomePage from "@pages/home";
import LoginPage from "@pages/login";
import MainLayout from "@shared/mainLayout";
import Question from "@pages/question";
import Video from "@pages/video";
import Mine from "@pages/mine";
import AuthRoute from "@router/authRoute";
import Personal from "@pages/personal";
import Search from "@pages/search";
import SearchResult from "@pages/searchResult";
import Article from "@pages/article";
import Chat from "@pages/chat";

// 创建并导出路由配置
export const router = createBrowserRouter([
  {
    path: "/", // 根路径
    element: <MainLayout />, // 主布局组件
    children: [
      { path: "", element: <Navigate to={"/0"} /> }, // 默认重定向到 "/0"
      { path: ":cid", element: <HomePage /> }, // 首页，动态路径参数
      { path: "question", element: <Question /> }, // 问题页面
      { path: "video", element: <Video /> }, // 视频页面
      {
        path: "mine", // “我的”页面，需要认证
        element: (
          <AuthRoute>
            <Mine />
          </AuthRoute>
        ),
      },
    ],
  },
  {
    path: "/personal", // 个人页面
    element: <Personal />,
  },
  {
    path: "/private", // 私有路由，需要认证
    element: <AuthRoute />,
    children: [
      {
        path: "personal", // 个人详情页面
        element: <Personal />,
      },
      {
        path: "chat", // 聊天页面
        element: <Chat />,
      },
    ],
  },
  {
    path: "/login", // 登录页面
    element: <LoginPage />,
  },
  {
    path: "/search", // 搜索页面
    element: <Search />,
  },
  {
    path: "/search/:key", // 搜索结果页面，动态路径参数
    element: <SearchResult />,
  },
  {
    path: "/article/:id", // 文章页面，动态路径参数
    element: <Article />,
  },
]);

// 默认导出函数组件，使用RouterProvider包裹路由配置
export default function AppRouter() {
  return <RouterProvider router={router} />;
}
